package com.demo2do.fms.admin.web.controller.data.cost;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.report.ExportType;
import com.demo2do.fms.admin.entity.report.data.RepairFeeReportSearch;
import com.demo2do.fms.admin.entity.search.data.RepairFeeSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.data.MasterDataService;
import com.demo2do.fms.admin.service.data.RepairFeeService;
import com.demo2do.fms.admin.utils.FormatUtils;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.authority.Organization;
import com.demo2do.fms.entity.data.business.VehicleFeature;
import com.demo2do.fms.entity.data.cost.RepairFee;

/**
 * 修理费管理
 * 
 * @author Wy
 *
 */
@Controller
@RequestMapping("/repair-fees")
@MenuSetting("menu-data")
public class RepairFeeController {
    
    @Autowired
    @Qualifier("repairFeeService")
    private RepairFeeService repairFeeService;
    
    @Autowired
    private MasterDataService masterDataService;
    
    /**
     * list  修理费
     * 
     * @param orgIds
     * @param page
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.GET)
    @MenuSetting("submenu-repair-fee")
    public ModelAndView list(@Secure(property = "orgIds") List<Long> orgIds, Page page){
       
       ModelAndView modelAndView = new ModelAndView("data/cost/repair-fee/repair-fee-list");
       
       int totals = repairFeeService.count(orgIds);
       if(totals > 0){
            
            List<RepairFee> repairFees= repairFeeService.list(orgIds, page.with(totals));
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("repairFees", repairFees);
            
       }
       
       return modelAndView;
       
     }
    
    /**
     * search
     * 
     * @param orgIds
     * @return
     */
    @RequestMapping(value = "/search" , method = RequestMethod.GET )
    @MenuSetting("submenu-repair-fee")
    public ModelAndView search(@Secure(property = "orgIds") List<Long> orgIds){
        
        ModelAndView modelAndView = new ModelAndView("data/cost/repair-fee/repair-fee-search");
        
        List<VehicleFeature> vehicleFeatures = masterDataService.listVehicleFeature();
        modelAndView.addObject("vehicleFeatures", vehicleFeatures);
        
        modelAndView.addObject("orgIds", orgIds);
        
        return modelAndView;
        
     }
    
    /**
     * on search
     * 
     * @param repairFeeSearch
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.GET , params = "filter")
    @MenuSetting("submenu-repair-fee")
    public ModelAndView onSearch(RepairFeeSearch repairFeeSearch, HttpServletRequest request, Page page) {

        ModelAndView modelAndView = new ModelAndView("data/cost/repair-fee/repair-fee-list");

        int totals = repairFeeService.count(RepairFee.class, repairFeeSearch.getFilter());
        if (totals > 0) {

            List<RepairFee> repairFees = repairFeeService.search(RepairFee.class, repairFeeSearch, page.with(totals));
            modelAndView.addObject("repairFees", repairFees);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));

        }

        return modelAndView;

    }
    
    /**
     * 查询修理费后设置
     * 
     * @param organization
     * @param vehicleFeature
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.GET , params = "prepare" ,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onPrepare(@Preloading("orgId") Organization organization,
                                              @Preloading("powerTypeId") VehicleFeature vehicleFeature){
        
        HttpResult httpResult = new HttpResult();
        
        List<RepairFee> repairFees = repairFeeService.list(organization, vehicleFeature);
            
        httpResult.success().data("repairFees", repairFees);
         
        return httpResult;
     }
    
    /**
     * 设置修理费
     * 
     * @param orgIds
     * @return
     */
    @RequestMapping(value = "/edit" , method = RequestMethod.GET )
    @MenuSetting("submenu-repair-fee")
    public ModelAndView edit(@Secure(property = "orgIds") List<Long> orgIds){
        
        ModelAndView modelAndView = new ModelAndView("data/cost/repair-fee/repair-fee-edit");
        
        //list all vehicle_features(动力类型)
        List<VehicleFeature> vehicleFeatures = masterDataService.listVehicleFeature();
        
        modelAndView.addObject("vehicleFeatures", vehicleFeatures);
        modelAndView.addObject("orgIds", orgIds);
        
        return modelAndView;
     }
    
    /**
     * 设置修理费
     * 
     * @param login
     * @param organization
     * @param vehicleFeature
     * @param rangeFroms
     * @param prices
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.PATCH , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEdit(@Secure Login login,
                                           @Preloading("organizationId") Organization organization,
                                           @Preloading("powerTypeId") VehicleFeature vehicleFeature,
                                           @RequestParam(name = "rangeFrom", required = false) List<Integer> rangeFroms,
                                           @RequestParam(name = "price", required = false) List<BigDecimal> prices) {
        
        HttpResult httpResult = new HttpResult();
        
        repairFeeService.edit(login.getId() , organization, vehicleFeature, rangeFroms, prices);
        
        httpResult.success().message("设置成功");
        
        return httpResult;
    }
    
    /**
     * 输出当前页记录
     * 
     * @param search
     * @param orgIds
     * @param page
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export(RepairFeeSearch search, 
                               @Secure(property = "orgIds") List<Long> orgIds, Page page) {

        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("data/cost/repair-fee-export.xlsx"));
        
        // 1. list result
        if (CollectionUtils.isEmpty(search.getOrganizationIds())) {
            search.setOrganizationIds(orgIds);
        }
        int totals = repairFeeService.count(RepairFee.class, search.getFilter());
        if (totals > 0) {
            List<RepairFee> repairFees = repairFeeService.search(RepairFee.class, search, page.with(totals));
            modelAndView.addObject("repairFees", repairFees);
        }
        
        // 2. attribute
        modelAndView.addObject("type", ExportType.CURRENT);

        // 3. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);

        return modelAndView;
        
    }
    
    /**
     * 输出所有记录
     * 
     * @param search
     * @param orgIds
     * @return
     */
    @RequestMapping(value = "/export-all", method = RequestMethod.GET)
    public ModelAndView exportAll(RepairFeeReportSearch search, @Secure(property = "orgIds") List<Long> orgIds) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("data/cost/repair-fee-export.xlsx"));
        
        // 1. list result 
        if (CollectionUtils.isEmpty(search.getOrganizationIds())) {
            search.setOrganizationIds(orgIds);
        }
        List<Map<String, Object>> results = repairFeeService.exportAll(search);
        modelAndView.addObject("results", results);
        
        // 2. attribute
        modelAndView.addObject("type", ExportType.ALL);
        
        // 3. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
}
